﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ThoiTrang.Business;
using ThoiTrang.Entities;
using ThoiTrang.Data;
using System.Text;

public partial class adm_survey : System.Web.UI.Page
{
    #region "Page functions"

    public string ckid = "";

    private void _BindGreeting()
    {
        a_User au=a_UserService.Geta_User(Request.Params["ckid"]);
        if (au != null)
        {
            this.lblGretting.Text = "Hi " + au.Firstname + " " + au.Lastname;
        }
    }

    private void _BindRepeater()
    {
        int suveryStt = int.Parse(this.drpStatus.SelectedValue);
        if (Request.Params["fn"] == null | (Request.Params["fn"] == "#" | Request.Params["fn"] == ""))
        {
            this.rptSurvey.DataSource = a_SurveyService.Geta_SurveyList_stt(suveryStt,a_SurveyColumns.Status, OrderDirection.DESC);
            this.rptSurvey.DataBind();
        }
        else
        {
            this.rptSurvey.DataSource = a_SurveyService.Geta_SurveyList_alphabet_stt(suveryStt,Request.Params["fn"].ToString(), a_SurveyColumns.Status, OrderDirection.DESC);
            this.rptSurvey.DataBind();
        }
    }

    private void _BindAlphabetSearcher()
    {
        DataTable dt = General.ExecSQLStatement("SELECT DISTINCT SUBSTRING(Question, 1, 1) AS Name FROM a_Surveys ORDER BY Name");
        this.rptAlphabetSearch.DataSource = dt;
        this.rptAlphabetSearch.DataBind();
    }

    private void _ReadParam()
    {
        try
        {
            if (Request.Params["catid"] != null && Request.Params["act"] == null)
            {
                a_Survey acy = a_SurveyService.Geta_Survey(int.Parse(Request.Params["catid"]));
                if (acy != null)
                {
                    this.drpStatus.SelectedValue = acy.Status.ToString();
                    this.txtBodyVn.Text = acy.Question;
                    this.btnUpdate.Visible = true;
                    this._BindRepeater();
                }
            }
            else if (Request.Params["catid"] != null && Request.Params["act"] == "del")
            {
                //Kiem tra xem co category nao khong?
                a_SurveyService.Deletea_Survey(int.Parse(Request.Params["catid"]));
                CommonFunc.ShowAlertMessage(this.lblAlert, "Delete sucessfully");
                this._BindRepeater();
            }
            else if (Request.Params["fn"] != null)
            {
                this._BindRepeater();
            }
        }
        catch (Exception ex)
        {
            //throw new exception here
        }
    }

    public string BindStt(byte stt)
    {
        if (stt == 0)
        {
            return "Pending";
        }
        else
        {
            return "Activated";
        }

    }

    public string CreateEditDeleteCommentLink(int catid,string title)
    {
        string hLink = "";
        if (Request.Params["ckid"] != null)
        {
            hLink = "<a href='adm_survey.aspx?ckid=" + Request.Params["ckid"].ToString() + "&catid=" + catid.ToString() + "{0}'><img src='../images/ico_edit.gif' alt='edit' width='12' height='12' /></a><a onclick=" + "\"" + "return confirm('Are you sure want to delete?')" + "\"" + "' href='adm_survey.aspx?ckid=" + Request.Params["ckid"].ToString() + "&catid=" + catid.ToString() + "&act=del" + "&fn={0}'><img src='../images/ico_delete.gif' alt='del' width='16' height='13' /></a><a href='adm_surveyoption.aspx?ckid=" + Request.Params["ckid"].ToString() + "&qid=" + catid.ToString() + "'>Responses</a>&nbsp;<a href='adm_surveycomment.aspx?ckid=" + Request.Params["ckid"].ToString() + "&qid=" + catid.ToString() + "&title=" + System.Web.HttpUtility.UrlEncode(title) + "'>Comments</a>";
        }
        else
        {
            hLink = "<a href='adm_survey.aspx?catid=" + catid.ToString() + "{0}'><img src='../images/ico_edit.gif' alt='edit' width='12' height='12' /></a><a href='adm_survey.aspx?catid=" + catid.ToString() + "&act=del" + "&fn={0}'><img src='../images/ico_delete.gif' alt='del' width='16' height='13' /></a><a href='adm_surveyoption.aspx?ckid=" + Request.Params["ckid"].ToString() + "&qid=" + catid.ToString() + "'>Responses</a>&nbsp;<a href='adm_surveycomment.aspx?ckid=" + Request.Params["ckid"].ToString() + "&qid=" + catid.ToString() + "&title=" + System.Web.HttpUtility.UrlEncode(title) + "'>Comments</a>";
        }
        if (Request.Params["fn"] != null && (Request.Params["fn"] != "#" | Request.Params["fn"] != ""))
        {
            hLink = hLink.Replace("{0}","&fn="+ Request.Params["fn"].ToString());
        }
        else
        {
            hLink = hLink.Replace("{0}", "");
        }
        return hLink;
    }

    public string GetActiveClass(string fn)
    {
        if (Request.Params["fn"] != null && Request.Params["fn"] == fn)
        {
            return "active";
        }
        else
        {
            return "";
        }
    }

    public string GetActiveClassAll()
    {
        if (Request.Params["fn"] == null)
        {
            return "active";
        }
        else
        {
            return "";
        }
    }

    public string _CreateLinkforAll()
    {
        if (Request.Params["ckid"] != null)
        {
            return "adm_survey.aspx?ckid=" + Request.Params["ckid"].ToString();
        }
        else
        {
            return "";
        }
    }

    private bool CheckisQuestionExistence(string quescontent)
    {
        foreach (a_Survey aso in a_SurveyService.Geta_SurveyList(a_SurveyColumns.Question, ThoiTrang.Data.OrderDirection.ASC))
        {
            if (aso.Question == quescontent)
            {
                return true;
            }
        }
        return false;
    }
    #endregion

    #region "Event"
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["ckid"] != null)
        {
            ckid = Request.Params["ckid"];
            
        }
        if (!this.IsPostBack)
        {
            this._BindRepeater();
            this._ReadParam();
            this._BindGreeting();
            this._BindAlphabetSearcher();
        }
    }

    protected void btnCommit_Click(object sender, EventArgs e)
    {
        try
        {
            if (!this.CheckisQuestionExistence(this.txtBodyVn.Text))
            {
                a_Survey acy = new a_Survey();
                acy.Question = this.txtBodyVn.Text;
                acy.Status = byte.Parse(this.drpStatus.SelectedValue);
                a_SurveyService.Createa_Survey(acy);
                CommonFunc.ShowAlertMessage(this.lblAlert, "Add new survey sucessfully");
                this._BindRepeater();
            }
            else
            {
                CommonFunc.ShowAlertMessage(this.lblAlert, "The question already existed");
            }
        }
        catch (Exception ex)
        {
            //throw new error
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            a_Survey acy = a_SurveyService.Geta_Survey(int.Parse(Request.Params["catid"]));
            if (acy != null)
            {
                if (this.drpStatus.SelectedValue == "1")
                {
                    try
                    {
                        a_Survey aTemp = a_SurveyService.Geta_SurveyList(a_SurveyColumns.Status, OrderDirection.DESC)[0];
                        aTemp.Status = 0;
                        a_SurveyService.Updatea_Survey(aTemp);
                    }
                    catch (Exception ex)
                    {

                    }

                }
                acy.Question = this.txtBodyVn.Text;
                acy.Status = byte.Parse(this.drpStatus.SelectedValue);
                this.btnUpdate.Visible = false;
                a_SurveyService.Updatea_Survey(acy);
                
                CommonFunc.ShowAlertMessage(this.lblAlert, "Update survey sucessfully");
                this._BindRepeater();
            }
            else
            {
                CommonFunc.ShowAlertMessage(this.lblAlert, "The question already existed");
            }
        }
        catch (Exception ex)
        {
            //throw new exception here
        }
    }

    protected void drpStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        this._BindRepeater();
    }
    #endregion 
   
    
    
}
